#include "ch32v10x.h"
#include <rtthread.h>
#include <rthw.h>
#include "drivers/pin.h"
#include "led_bsp.h"

#define LED_PIN         (14)      //PA0
#define CTL_LED_PIN     (15)      //PA1

#define ON_PULSE        (100)
#define OFF_PULSE       (0)


UINT8 led_toggle_state = 0;
UINT8 ctl_led_toggle_state = 0;

UINT8 led_breath_dir = 0;
UINT8 ctl_led_breath_dir = 0;

UINT16 led_breath_counts = 0;
UINT16 ctl_led_breath_counts = 0;

void led_set(UINT16 pulse)
{
    if(pulse > ON_PULSE || pulse < OFF_PULSE)
    {
        return;
    }
    TIM_SetCompare1(TIM2, pulse);
}
void ctl_led_set(UINT16 pulse)
{
    if(pulse > ON_PULSE || pulse < OFF_PULSE)
    {
        return;
    }
    TIM_SetCompare2(TIM2, pulse);
}

void led_on(void)
{
    led_set(ON_PULSE);
}

void led_off(void)
{
   led_set(OFF_PULSE);
}

void ctl_led_on(void)
{
   ctl_led_set(ON_PULSE);
}

void ctl_led_off(void)
{
   ctl_led_set(OFF_PULSE);
}

void led_toggle(void)
{
    if(led_toggle_state == 1)
    {
        led_on();
        led_toggle_state = 0;
    }
    else
    {
        led_off();
        led_toggle_state = 1;
    }
}

void ctl_led_toggle(void)
{
    if(ctl_led_toggle_state == 1)
    {
        ctl_led_on();
        ctl_led_toggle_state = 0;
    }
    else
    {
        ctl_led_off();
        ctl_led_toggle_state = 1;
    }
}

void led_breath(UINT16 tick)
{
    if(led_breath_dir == 0)
        led_breath_counts += tick;
    else
        led_breath_counts -= tick;
    if(led_breath_counts >= ON_PULSE)
    {
        led_breath_counts = ON_PULSE;
        led_breath_dir = 1;
    }
    else if(led_breath_counts <= OFF_PULSE)
    {
        led_breath_counts = OFF_PULSE;
        led_breath_dir = 0;
    }
    led_set(led_breath_counts);
}

void ctl_led_breath(UINT16 tick)
{
    if(ctl_led_breath_dir == 0)
        ctl_led_breath_counts += tick;
    else
        ctl_led_breath_counts -= tick;
    if(ctl_led_breath_counts >= ON_PULSE)
    {
        ctl_led_breath_counts = ON_PULSE;
        ctl_led_breath_dir = 1;
    }
    else if(ctl_led_breath_counts <= OFF_PULSE)
    {
        ctl_led_breath_counts = OFF_PULSE;
        ctl_led_breath_dir = 0;
    }
    ctl_led_set(ctl_led_breath_counts);
}

void led_init(void)
{
    rt_pin_mode(LED_PIN,PIN_MODE_OUTPUT_AF_PP);
    rt_pin_mode(CTL_LED_PIN,PIN_MODE_OUTPUT_AF_PP);

    TIM_OCInitTypeDef TIM_OCInitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
    RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2 , ENABLE );
    TIM_TimeBaseInitStructure.TIM_Period = 72-1;
    TIM_TimeBaseInitStructure.TIM_Prescaler = ON_PULSE;
    TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit( TIM2, &TIM_TimeBaseInitStructure);

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = OFF_PULSE;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC1Init( TIM2, &TIM_OCInitStructure );
    TIM_OC2Init( TIM2, &TIM_OCInitStructure );

    TIM_CtrlPWMOutputs(TIM2, ENABLE );

    TIM_OC1PreloadConfig( TIM2, TIM_OCPreload_Disable );
    TIM_OC2PreloadConfig( TIM2, TIM_OCPreload_Disable );

    TIM_ARRPreloadConfig( TIM2, ENABLE );
    TIM_Cmd( TIM2, ENABLE );

    led.on = led_on;
    led.off = led_off;
    ctl_led.on = ctl_led_on;
    ctl_led.off = ctl_led_off;

}
INIT_COMPONENT_EXPORT(led_init);


